<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8"/>
    <link rel="stylesheet" href="../js/layui/css/layui.css"/>
    <link rel="stylesheet" href="../css/qq.css"/>
    <script src="../js/layui/layui.all.js"></script>
    <script src="../js/layui/layui.js"></script>
    <title>网站采集器</title>
</head>
<body>
<div class="center">
    <div class="form-box">
        <form class="layui-form">
            <div class="layui-form-item">
                <h4 style="color: red">* 一般一页可重复采集多次，直至数据不再增长;当数据采集不到，则需程序服务器访问【目标网站】已解除IP封禁</h4>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">目标网站</label>
                <div class="layui-input-block">
                    <input type="radio" class="webType" name="sex" lay-filter="source" value="1" title="58同城" checked/>
                    <input type="radio" class="webType" name="sex" lay-filter="source" value="2" title="赶集网"  />
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">一级分类</label>
                <div class="layui-input-block" style="width: 300px;">
                    <select name="city" id="oneCategory" lay-filter="oneCategorySelect">

                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">二级分类</label>
                <div class="layui-input-block" style="width: 300px;">
                    <select name="city" id="twoCategory" lay-filter="twoCategorySelect">

                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">采集规则</label>
                <div class="layui-input-block">
                    <input type="radio" name="sex1" value="手机号码" title="手机号码" checked>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">采集第</label>
                <div class="layui-input-inline">
                    <input type="text" id="dataPage" value="1" name="title" placeholder="" autocomplete="off" class="layui-input">
                </div>
                <label class="layui-form-label" style="padding-left: 0;text-align: left">页后停止</label>
            </div>

            <fieldset class="layui-elem-field layui-field-title" style="margin-top: 50px;">
                <legend>采集进度</legend>
            </fieldset>
            <div class="layui-form-item">
                <div class="layui-progress layui-progress-big" lay-showpercent="true" lay-filter="demo">
                    <div class="layui-progress-bar layui-bg-red" lay-percent="0%"></div>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit lay-filter="formDemo" data-type="loading">采集</button>
                    <!--<button class="layui-btn" lay-submit lay-filter="exportTxt">导出</button>-->
                    <a class="layui-btn" href="/exportTxt" download>导出</a>

                    <button class="layui-btn">清空</button>
                </div>
            </div>

        </form>
    </div>
    <div class="data-list">
        <div class="title">
            <p class="id-title">ID</p>
            <p class="phone-title">手机</p>
        </div>
        <ul class="list" id="phoneList">

        </ul>
    </div>
</div>



<script>
    layui.use(['form','jquery','layer','element'],function () {
        var $ = layui.jquery;
        var form = layui.form;
        var layer = layui.layer;
        var element = layui.element;
        var twoCategorySelectValue = "";
        form.render();
        getSelectOne(1,0);
        //radio选中事件
        form.on('radio(source)', function (data) {
            console.log(data.value);
            getSelectOne(data.value,0);
        });
        //一级分类触发事件
        form.on('select(oneCategorySelect)', function(data){
            console.log(":"+data.value);
            let parentId = data.value;
            let type = $('.webType:checked ').val();
            getSelectTwo(type,parentId);
        });

        //二级分类触发事件
        form.on('select(twoCategorySelect)', function(data){
            if(twoCategorySelectValue!=data.value){
                console.log(":"+data.value);
                twoCategorySelectValue=data.value;
                $.get('delRedisCategoryUrl',{},function () {
                    
                });
            }
        });

        //获取一级分类
        function getSelectOne(type,parentId) {
            $("#oneCategory").html("");
            $.post("/category/get",{type:type,parentId:parentId},function (data) {
                var option1 = "";
                $.each(data.categories, function(key,val) {
                    //option1 += $("<option>").val(val.id).text(val.title);
                    option1 += "<option value='"+val.id+"'>"+val.title+"</option>";
                });

                $("#oneCategory").append(option1);
                form.render('select');
                $("#oneCategory").get(0).selectedIndex=0;

                getSelectTwo(type,$("#oneCategory").val());
            })
        }
        //获取二级分类
        function getSelectTwo(type,parentId) {
            $("#twoCategory").html("");
            $.post("/category/get",{type:type,parentId:parentId},function (data) {
                $.each(data.categories, function(key,val) {
                    var option1 = $("<option>").val(val.id).text(val.title);
                    $("#twoCategory").append(option1);
                    form.render('select');
                });
                $("#twoCategory").get(0).selectedIndex=0;
            })
        }
        //采集事件
        form.on("submit(formDemo)",function (data) {
            let categoryId = $("#twoCategory").val();
            let page = $("#dataPage").val();

            var othis = $(this), type = $(this).data('type');
            active[type] ? active[type].call(this, othis) : '';


            return false;
        });


        function extracted(categoryId,page) {
            $.ajaxSettings.async = false;
            var code = "";
            $.get("/grabPhone", {categoryId: categoryId, page: page}, function (data) {
                //console.log(data.phoneVos);
                if (data.code === 0) {
                    let phoneList = "";
                    for (var i = 0; i < data.phoneVos.length; i++) {
                        var num = i + 1;
                        phoneList += " <li class=\"list-item\">\n" +
                            "                <p class=\"id-title\">" + num + "</p>\n" +
                            "                <p class=\"phone-title\">" + data.phoneVos[i].phone + "</p>\n" +
                            "            </li>"
                    }
                    $("#phoneList").html(phoneList);
                }
                //layer.close(layerIndex); //完成加载后关闭loading
                layer.msg(data.msg);
                code = data.code;
            });
            return code;
            $.ajaxSettings.async = true;
        }

        form.on("submit(exportTxt)",function (data) {
            location.href ="/exportTxt";
            return false;
        });


        var layerIndex = "";
        //触发事件
        var active = {

            loading: function(othis){
                layerIndex = layer.load(0, { shade: [0.01, '#fff'] });
                var DISABLED = 'layui-btn-disabled';
                if(othis.hasClass(DISABLED)) return;

                //模拟loading
                var n = 0, timer = setInterval(function(){
                    let categoryId = $("#twoCategory").val();
                    let page = $("#dataPage").val();
                    let returncode = "";
                    if(n<100){
                        returncode = extracted(categoryId,page);
                    }
                    if(returncode===0){
                        n = n + 20;
                    }else{
                        n=101;
                    }

                    if(n>100){
                        n = 100;
                        clearInterval(timer);
                        othis.removeClass(DISABLED);
                        layer.close(layerIndex);
                    }
                    element.progress('demo', n+'%');

                }, 300+Math.random()*3000);
                //layer.close(layerIndex);
                othis.addClass(DISABLED);
            }
        };
    })
</script>
</body>
</html>